package com.study.register.service;


import com.study.common.entity.LoginException;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.common.utils.AliyunMessageUtils;
import com.study.login.dao.LoginDao;
import com.study.main.entity.Information;
import com.study.register.dao.RegisterDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/8/31 13:58
 * @Description
 */
@Service(value = "registerService")
public class RegisterServiceImpl implements RegisterService{
    @Autowired
    @Qualifier(value = "registerDao")
    private RegisterDao registerDao;
    @Autowired
    private LoginDao loginDao;
    /**
     *  添加用户信息
     * @param user 用户信息
     * @return void
     *
     */
    @Override
    public void save(User user) {
        registerDao.add(user);
        saveInformation(user.getUserName());
    }

    private void saveInformation(String userName) {
       User user =  loginDao.getUserByUserName(userName);
        Information information = new Information();
        information.setUserId(user.getUserId());
        information.setCredit(0);
        information.setGrade(0);
        registerDao.add(information);
    }

    /**
     * 发送短信判断用户是否在一分钟内发红过短信
     * @param telNumber 用户输入的电话号码
     * @param session
     * @return java.lang.String
     *
     */
    @Override
    public String sendCode(String telNumber, HttpSession session) {
            String code = null;
            String 	result;
            try {
                //判断phoneNumber session是否存在，和是否和以前的手机号相等
                if(session.getAttribute(telNumber)== null && session.getAttribute(telNumber)!=telNumber){
                    Long  a = data();
                    String name = telNumber;
                    session.setAttribute(name, a);
                }else{
                    String name = telNumber;
                    Long  a = data();
                    Long  b = (Long) session.getAttribute(name);
                    session.removeAttribute(name);
                    if(a-b<=60){
                        return "发送请求过于平凡请耐心等待";
                    }
                }
                code=AliyunMessageUtils.sendMsg(telNumber);
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                throw new LoginException(ExceptionInfo.SENDMSG);
                }
        return code;
        }

    @Override
    public void updateByPassword(User user) {
       registerDao.updateByPassword(user);
    }

    @Override
    public User findByTelNumber(String telNumber) {
        return registerDao.findByTelNumber(telNumber);
    }

    @Override
    public User findByUserName(String userName) {
        return registerDao.findByUserName(userName);
    }

    @Override
    public User findById(int userId) {
        Information information = (Information) registerDao.findById(Information.class,userId);
        return (User) registerDao.findById(User.class,information.getUserId());
    }

    //获取当前时间
    private Long data(){
        Date date = new Date();
        SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");
        Long  a = Long.parseLong(s.format(date));
        return a;
    }
}
